package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyDao;
import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.CompanyType;

public class CompanyDaoImpl extends HibernateDaoSupport implements CompanyDao {

	public CompanyDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Object saveCompany(Company company) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(company);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Company checkEmail(final String comEmail) {
		// TODO Auto-generated method stub
		Object c=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Company where comEmail='"+comEmail+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return (Company)c;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Company checkName(final String comName) {
		// TODO Auto-generated method stub
		@SuppressWarnings("rawtypes")
		Object c=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Company where comName='"+comName+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return (Company)c;
	}

	@Override
	public boolean updateCompany(Company company) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().update(company);
			return true;
			
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	@Override
	public Company selectCompany(String companyId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Company.class, companyId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Company comLogin(final String name,final String pwd) {
		// TODO Auto-generated method stub
		Object c=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="from Company where comEmail='"+name+"' and comPwd='"+pwd+"'";
				Query query=session.createQuery(sql);
				return query.uniqueResult();
			}
		});
		return (Company)c;
		
	}

	@Override
	public Company checkBankCard(final String bankCard) {
		// TODO Auto-generated method stub
	
		@SuppressWarnings({ "unchecked", "rawtypes" })
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Company where bankCard='"+bankCard+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return (Company)object;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<CompanyType> selectComType() {
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from CompanyType";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<CompanyType>)object;
	}

	@Override
	public CompanyType getComType(int comtypeId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(CompanyType.class, comtypeId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateIDCard(final String comId,final String cardImage) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Company set idCard='"+cardImage+"' where comId='"+comId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

}
